﻿Public Class SettingLocation
    Inherits baseForm
    Private Sub loadUser()
        Dim dt As DataTable
        If Utils.executeSP("proc_searchall", New Object() {"*", "vuserrole", "username", ""}, dt) Then
            Utils.setgrid(DGViewUser, , dt)
        End If
    End Sub
    Private Sub loadLocation()
        Dim dt As DataTable
        dt = Utils.SearchAllLike("lokasiitem", "namaretail", "")
        If Utils.RowExist(dt) Then
            Dim objcheck As New DataColumn
            objcheck.DataType = System.Type.GetType("System.Boolean")
            objcheck.ColumnName = "Select"
            objcheck.DefaultValue = False

            dt.Columns.Add(objcheck)
            
        End If
        

        Utils.setgrid(DGViewLocation, , dt)
        DGViewLocation.Columns("Select").DisplayIndex = 0
    End Sub
    Private Sub SettingLocation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadUser()
        loadLocation()
    End Sub

    Private Sub DGViewLocation_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGViewLocation.CellContentClick
        If Not e.RowIndex = -1 Then
            If DGViewLocation.DataSource IsNot Nothing Then
                If DGViewLocation.Columns(e.ColumnIndex).Name = "Select" Then
                    If Not IsDBNull(DGViewLocation(e.ColumnIndex, e.RowIndex)) Then
                        If DGViewLocation(e.ColumnIndex, e.RowIndex).Value Then
                            DGViewLocation(e.ColumnIndex, e.RowIndex).Value = False
                        Else
                            DGViewLocation(e.ColumnIndex, e.RowIndex).Value = True
                        End If
                        'setdtcetak()
                    End If
                End If
            End If
        End If
    End Sub
    Function validasi() As Boolean
        Dim rcount As Integer = 0

        If DGViewLocation.DataSource IsNot Nothing Then
            If DGViewLocation.RowCount > 0 Then
                For Each dgr As DataGridViewRow In DGViewLocation.Rows
                    If dgr.Index > -1 Then
                        If dgr.Cells("Select").Value = True Then
                            rcount += 1
                        End If
                    End If
                Next
            End If
        End If
        If rcount > 0 Then
            Return True
        Else
            Return False
        End If
    End Function
    Private Sub MbGlassButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MbGlassButton1.Click
        Dim retval As Integer = MessageBox.Show("Apakah anda yakin akan menyimpan data ?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
        If Not retval = DialogResult.Yes Then
            Exit Sub
        End If
        Dim kodeuser As String
        If DGViewUser.SelectedRows().Count > 0 Then
            kodeuser = DGViewUser.SelectedRows()(0).Cells(0).Value.ToString()
        Else
            Utils.getmessagesystem(1, "User Belum Dipilih!!")
            Exit Sub
        End If


        If Utils.exec_SP("proc_deleteuserlokasi", New Object() {kodeuser}) Then
            If Not validasi() Then
                Utils.getmessagesystem(0, "Execute Finish")
                Exit Sub
            End If
            If DGViewLocation.DataSource IsNot Nothing Then
                If DGViewLocation.RowCount > 0 Then
                    For Each dgr As DataGridViewRow In DGViewLocation.Rows
                        If dgr.Index > -1 Then
                            If dgr.Cells("Select").Value = True Then
                                If Utils.exec_SP("proc_setuserlokasi", New Object() {kodeuser, dgr.Cells("koderetail").Value}) Then

                                End If
                            End If
                        End If
                    Next
                    Utils.getmessagesystem(0, "Execute Finish")
                End If
            End If
        End If

    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        If DGViewLocation.DataSource IsNot Nothing Then
            If DGViewLocation.RowCount > 0 Then
                For Each dgr As DataGridViewRow In DGViewLocation.Rows
                    If dgr.Index > -1 Then
                        dgr.Cells("Select").Value = True
                    End If
                Next
            End If
        End If
    End Sub

    Private Sub LinkLabel2_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
        If DGViewLocation.DataSource IsNot Nothing Then
            If DGViewLocation.RowCount > 0 Then
                For Each dgr As DataGridViewRow In DGViewLocation.Rows
                    If dgr.Index > -1 Then
                        dgr.Cells("Select").Value = False
                    End If
                Next

            End If
        End If
    End Sub

    Private Sub DGViewUser_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGViewUser.CellClick
        Dim dslocation As New DataTable
        Dim dtul As New DataTable

        If DGViewUser.RowCount < 0 Then
            Utils.getmessagesystem(1, "List User Kosong")
            Exit Sub
        End If

        If DGViewLocation.DataSource IsNot Nothing Then
            dslocation = DGViewLocation.DataSource
            If Not Utils.RowExist(dslocation) Then
                Utils.getmessagesystem(1, "List Lokasi Kosong")
                Exit Sub
            End If
        Else
            Utils.getmessagesystem(1, "List Lokasi Kosong")
            Exit Sub
        End If


        If Utils.executeSP("proc_getuserlokasi", New Object() {DGViewUser("kodeuser", e.RowIndex).Value}, dtul) Then
            If Utils.RowExist(dtul) Then
                For Each dr As DataRow In dtul.Rows
                    For Each dgr As DataGridViewRow In DGViewLocation.Rows
                        If dgr.Cells("koderetail").Value = dr("koderetail") Then
                            dgr.Cells("Select").Value = True
                            Exit For
                        End If
                    Next
                Next
            End If
        End If
    End Sub

    Private Sub MbGlassButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MbGlassButton3.Click
        loadUser()
        loadLocation()
    End Sub

    Private Sub btnAddRole_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRole.Click
        Dim DialogLokasi As New DialogLokasi
        DialogLokasi.ShowDialog()
    End Sub
End Class
